#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>          /* ioctl */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "vtl.h"

int main(int argc, char **argv) {
    int f,i;
    uint32_t ret = 0;
    char *bigbuf = malloc(10000);
    memset(bigbuf, 0, 10000);
    for (f=0; f<500; f++)
        strcat(bigbuf, "This is my string ");

    f = open("VTL", O_RDWR);

    printf("Opened device...%d\n", f);
    for (i=0; i<10; i++) {
        printf("Sending IOCTL CREATE_TAPE, ret=%d. ", ioctl(f, IOCTL_CREATE_TAPE, &ret));
        printf("Created tape: %d\n", ret);
        printf("Sending IOCTL REPLACE_TAPE: %d\n", ioctl(f, IOCTL_REPLACE_TAPE, &ret));
        printf("Writing...returned: %d bytes\n", write(f, bigbuf, 10000));
    }

    for (i=0; i<10; i++) {
        printf("Sending IOCTL REPLACE_TAPE: %d\n", ioctl(f, IOCTL_REPLACE_TAPE, &i));
        printf("Sending IOCTL REWIND_TAPE: %d\n", ioctl(f, IOCTL_REWIND_CURRENT_TAPE));
        printf("Reading...returned: %d bytes\n", read(f, bigbuf, 10000));
    }

    for (i=0; i<10; i++) {
        printf("Sending IOCTL DELETE_TAPE: %d\n", ioctl(f, IOCTL_DELETE_TAPE, &i));

    }

    printf("Reading...returned: %d bytes\n", read(f, bigbuf, 10000));
    printf("Result: %s\n", bigbuf);
    printf("Sending IOCTL REWIND_TAPE: %d\n", ioctl(f, IOCTL_REWIND_CURRENT_TAPE));

    printf("Closing..\n");
    close(f);
    return 0;
}

